﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data.SqlClient;

using JM.JCY.BusinessEntity;
using JM.JCY.Common;
using JM.JCY.BusinessEntity.DTOs.Notification;


namespace JM.JCY.DataAccess
{
    public class NotificationDA : JCYDataAccessBasecs
    {
        /// <summary>
        /// Insert notification base on DataRowState = Added
        /// </summary>
        /// <param name="Table">Table of notifications</param>
        public int InsertNotification(SingleNotification NotificationObj)
        {
            int notificationId = -1;

            using (DbCommand comm = base.Helper.BuildDbCommand("P_Notification_Insert"))
            {
                base.Helper.AssignParameterValues(comm, NotificationObj.SenderId, NotificationObj.RecipientId, NotificationObj.RedirectUrl, NotificationObj.ActualContent, NotificationObj.DisplayContent);
                base.Helper.ExecuteNonQuery(comm);
                return notificationId = Convert.ToInt32(base.Helper.GetParameterValue(comm, "@notificationId"));
            }
        }

        public int InsertNotifications(MultipleNotifications NotificationObj)
        {
            int cnt = 0;

            using (DbCommand comm = base.Helper.BuildDbCommand("P_Notification_Insert"))
            {
                foreach (int recipientId in NotificationObj.RecipientId)
                {
                    base.Helper.AssignParameterValues(comm,NotificationObj.SenderId, recipientId, NotificationObj.RedirectUrl, NotificationObj.ActualContent, NotificationObj.DisplayContent);
                    cnt += base.Helper.ExecuteNonQuery(comm);
                }
                return cnt;
            }
        }

        /// <summary>
        /// Get top 8 unread notifications
        /// </summary>
        /// <param name="staffID"></param>
        /// <returns></returns>
        public NotificationDataSet.NotificationTableDataTable GetTopXNotifications(int staffId)
        {
            NotificationDataSet.NotificationTableDataTable table = new NotificationDataSet.NotificationTableDataTable();
            base.Helper.Fill(table, "P_Notification_GetXNumber_S", staffId);
            return table;
        }

        /// <summary>
        /// Get last 30 days unread notifications
        /// </summary>
        /// <param name="staffID"></param>
        /// <returns></returns>
        public NotificationDataSet.NotificationTableDataTable GetLastThirtyDaysNotifications(int staffId)
        {
            NotificationDataSet.NotificationTableDataTable table = new NotificationDataSet.NotificationTableDataTable();
            base.Helper.Fill(table, "P_Notification_GetLastThirtyDays_S", staffId);
            return table;
        }

        /// <summary>
        /// Update notification  status = true base on DataRowState = Modified
        /// </summary>
        /// <param name="Table">Table of notifications</param>
        public void MarkAsRead(int[] NotificationIds)
        {
            try
            {
                using (DbCommand comm = base.Helper.BuildDbCommand("P_Notification_MarkAsRead"))
                {
                    foreach (int id in NotificationIds)
                    {
                        base.Helper.AssignParameterValues(comm, id);
                        base.Helper.ExecuteNonQuery(comm);
                    }
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// mark all notifcation as read base on recipient
        /// </summary>
        /// <param name="RecipientId"></param>
        public void MarkAllAsRead(string RecipientId)
        {
            using (DbCommand comm = base.Helper.BuildDbCommand("P_Notification_MarkAllAsRead"))
            {
                base.Helper.AssignParameterValues(comm, RecipientId);
                base.Helper.ExecuteNonQuery(comm);
            }
        }

        public int GetUnreadCount(int recipientId)
        {
            return base.Helper.ExecuteScalar<int>("P_Notification_Poll_Count_S", recipientId);
        }
    }
}
